/**
 * Created by shuaiyin on 2019/3/6.
 */

import axios from 'axios'
import config from '../plugins/config.vue'


let baseSU = config.http_su_ip + '/v2';
let base = config.http_ip + '/v2';


axios.interceptors.request.use(config => {
    return config;
}, err => {
    //Message.error('请求超时!');
    // return Promise.resolve(err);
})

axios.interceptors.response.use(data => {

    //window.console.log("data " + data);

    if (data.status && data.status == 200 && data.data.status == 500) {
        //Message.error({message: data.data.msg});
        return;
    }
    if (data.data.msg) {
        //Message.success({message: data.data.msg});
    }
    return data;
}, err => {

    //window.console.log("err " + err.response.status);

    if (err.response.status === 500) {
        //Message.error('鉴权失败 请重新登录');

        // router.push({
        //     path:'/login',
        // })

    } else if (err.response.status === 403) {
        //Message.error('权限不足,请联系管理员!');
    } else if (err.response.status === 401) {
        //Message.error({message: err.response.data.msg});
    } else if (err.response.status === 504 || err.response.status === 404) {
        //Message.error('服务器被吃了⊙﹏⊙∥');
    } else {
        if (err.response.data.msg) {
            //Message.error({message: err.response.data.msg});
        }else{
            //Message.error('未知错误!');
        }
    }
    // return Promise.resolve(err);
})

export const postRequest = (url, params) => {

    return axios({
        method: 'post',
        url: `${base}${url}`,
        data: params,
        transformRequest: [function (data) {
            let ret = ''
            for (let it in data) {
                ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
            }
            return ret
        }],
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        },
        withCredentials: true
    });
}

export const postSURequest = (url, params) => {

    return axios({
        method: 'post',
        url: `${baseSU}${url}`,
        data: params,
        transformRequest: [function (data) {
            let ret = ''
            for (let it in data) {
                ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
            }
            return ret
        }],
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        },
        withCredentials: true
    });
}

export const uploadFileRequest = (url, params) => {
    return axios({
        method: 'post',
        url: `${base}${url}`,
        data: params,
        headers: {
            'Content-Type': 'multipart/form-data'
        },
        withCredentials: true
    });
}
export const putRequest = (url, params) => {
    return axios({
        method: 'put',
        url: `${base}${url}`,
        data: params,
        transformRequest: [function (data) {
            let ret = ''
            for (let it in data) {
                ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
            }
            return ret
        }],
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        },
        withCredentials: true
    });
}
export const deleteRequest = (url) => {
    return axios({
        method: 'delete',
        url: `${base}${url}`,
        withCredentials: true
    });
}
export const getRequest = (url) => {
    return axios({
        method: 'get',
        url: `${base}${url}`,
        withCredentials: true
    });
}

